package drawings;
import lottery.office.*;
import games.DuzyLotek;

import java.text.DateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Locale;

public class DuzyLotekDrawingMachine extends DrawingMachine {
	private Drawing drawing;

	public Drawing getDrawing() {
		return drawing;
	}

	public Drawing draw() {
		Locale locale = new Locale("pl", "PL");
		//Calendar cal = Calendar.getInstance(locale);
		//System.out.println(cal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, locale));
		Date date = new Date();
		DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT, locale);
		String dateStr = formatter.format(date);
		//dateStr = formatter.format(date);
		Drawing.setNumberOfDrawings(Drawing.getNumberOfDrawings() + 1);

		int[] tableOfBallsToDrawFrom = new int[DuzyLotek.numberOfBallsToDrawFrom];
		int[] tableOfDrawedBalls = new int[DuzyLotek.numberOfBallsToDraw];

		for (int i = 0; i < tableOfBallsToDrawFrom.length; i++) {
			tableOfBallsToDrawFrom[i] = i + 1;
		}

		int numberOfBallsToDrawFrom = DuzyLotek.numberOfBallsToDrawFrom;
		for (int i = 0; i < DuzyLotek.numberOfBallsToDraw; i++) {
			int index = (int) (Math.random() * numberOfBallsToDrawFrom);

			tableOfDrawedBalls[i] = tableOfBallsToDrawFrom[index];
			tableOfBallsToDrawFrom[index] = tableOfBallsToDrawFrom[numberOfBallsToDrawFrom - 1];
			numberOfBallsToDrawFrom--;

		}
		
		
		drawing = new DuzyLotek(Drawing.getNumberOfDrawings(), dateStr, tableOfDrawedBalls);
		
		LotteryDatabase db = new LotteryDatabase();
		db.writeDrawing(Drawing.getNumberOfDrawings() + " | " + dateStr + " | " + Arrays.toString(tableOfDrawedBalls) + "\n");
		
		return drawing;
	}
}
